<template>
  <div class="container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top: 10px">
      <el-row>
        <el-button type="primary" size="small" @click="showAdd = true"
          >添加分类</el-button
        >
      </el-row>
      <el-table
        border
        :data="list"
        row-key="cat_id"
        style="margin-top: 20px"
        size="small"
        :indent="20"
      >
        <el-table-column
          label="#"
          type="index"
          align="center"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cat_name"> </el-table-column>
        <el-table-column label="是否有效" prop="cat_deleted">
          <template #default="{ row }">
            <i
              class="el-icon-success"
              style="color: lightgreen"
              v-if="!row.cat_deleted"
            ></i>
            <i v-else class="el-icon-error" style="color: lightcoral"></i>
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="cat_level">
          <template #default="{ row }">
            <el-tag v-if="row.cat_level === 0">一级</el-tag>
            <el-tag v-else-if="row.cat_level === 1" type="success">二级</el-tag>
            <el-tag v-else type="warning">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="editCate(row.cat_id)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="delCate(row.cat_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row style="margin-top: 10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="params.total"
          :current-page="params.pagenum"
          :page-size="params.pagesize"
          :page-sizes="[5, 10, 15, 20]"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <el-dialog
      :visible.sync="showAdd"
      :title="addForm.cat_id ? '修改分类' : '添加分类'"
    >
      <el-form
        label-width="100px"
        :model="addForm"
        :rules="addRules"
        ref="addForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" v-if="!addForm.cat_id">
          <el-cascader
            v-model="selectList"
            :props="cateProps"
            :options="parentCateList"
            style="width: 100%"
            clearable
            @change="changeParentList"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row>
          <el-button @click="addCancel">取消</el-button>
          <el-button type="primary" @click="addConfirm">确定</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  categories,
  addCategories,
  delCategories,
  CateDetail,
  editCate
} from '@/api/goods'
export default {
  data () {
    return {
      list: [],
      params: {
        type: '',
        pagenum: 1,
        pagesize: 10,
        total: 0
      },
      showAdd: false,
      addForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addRules: {
        cat_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      cateProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      },
      parentCateList: [],
      selectList: []
    }
  },
  created () {
    this.getTableList()
    this.getParentCateList()
  },
  methods: {
    async getTableList () {
      const res = await categories(this.params)
      this.params.total = res.total
      this.list = res.result
    },
    handleSizeChange (newSize) {
      this.params.pagesize = newSize
      this.getTableList()
    },
    handleCurrentChange (newPage) {
      this.params.pagenum = newPage
      this.getTableList()
    },
    async getParentCateList () {
      const res = await categories({ type: 2 })
      this.parentCateList = res
    },
    changeParentList (val) {
      this.addForm.cat_pid = val[val.length - 1]
      this.addForm.cat_level = val.length
    },
    addCancel () {
      this.$refs.addForm.resetFields()
      setTimeout(() => {
        this.addForm = {
          cat_pid: 0,
          cat_name: '',
          cat_level: 0
        }
      }, 500)
      this.showAdd = false
    },
    async addConfirm () {
      try {
        await this.$refs.addForm.validate()
        if (this.addForm.cat_id) {
          await editCate({ id: this.addForm.cat_id, ...this.addForm })
          this.$message.success('修改成功')
        } else {
          await addCategories(this.addForm)
          this.$message.success('添加成功')
        }
        this.showAdd = false
        this.getTableList()
      } catch (err) {
        console.log(err)
      }
    },
    async delCate (id) {
      try {
        await this.$confirm('此操作将永久删除该分类,是否继续', {
          title: '删除分类',
          type: 'warning'
        })
        await delCategories(id)
        this.$message.success('删除成功')
        this.getTableList()
      } catch (err) {
        console.log(err)
      }
    },
    async editCate (id) {
      this.addForm = await CateDetail(id)
      this.showAdd = true
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
  background-color: #f5f7f8;
}
</style>
